local zhengyun = fk.CreateSkill{
  name = "setam__zhengyun",
}

zhengyun:addEffect("viewas", {
  pattern = "chasing_near",
  anim_type = "control",
  mute_card = false,
  prompt = function ()
    return string.format(Fk:translate("#setam__zhengyun-prompt"), 1, Fk:translate("chasing_near"))
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected > 0 then return false end
    return Fk:getCardById(to_select).is_damage_card
  end,
  view_as = function (self, player, cards)
    if #cards == 0 then return end
    local card = Fk:cloneCard("chasing_near")
    card.skillName = zhengyun.name
    card:addSubcards(cards)
    return card
  end,
  after_use = function(self, player, use)
    local room = player.room
    local logic = room.logic
    local card_names = { "ex_nihilo", "reinforcement", "3" }
    for i, card_name in ipairs(card_names) do
      local cards = {}
      local previous_use = logic:getEventsByRule(GameEvent.CardEffect, 1, function(e)
        if e.data then
          return e.data.card == use.card
        end
      end)[1]
      if previous_use == nil then return end
      logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
        local c_event = e:findParent(GameEvent.CardEffect, false, 2)
        if previous_use ~= c_event then return false end
        for _, single in ipairs(e.data) do
          for _, info in ipairs(single.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card and room:getCardOwner(card) == player and card.is_damage_card then
              table.insert(cards, info.cardId)
            end
          end
        end
      end)
      -- p(cards)
      if card_name == "3" then
        local first_card_names = {}
        room.logic:getEventsOfScope(GameEvent.UseCard, 3, function (e)
          local first_use = e.data
          if first_use.from == player and use.card:isCommonTrick() then
            table.insertIfNeed(first_card_names, first_use.card.name)
            return true
          end
          return false
        end, Player.HistoryTurn)
        for _, viewas_name in ipairs(first_card_names) do
          -- p(cards)
          local seq_use = room:askToUseVirtualCard(player,{
            name = viewas_name,
            skill_name = zhengyun.name,
            prompt = string.format(Fk:translate("#setam__zhengyun-prompt"), 1, Fk:translate(viewas_name)),
            card_filter = {
              n = 1,
              cards = cards
            },
          })
          if not seq_use then break end
          table.removeOne(cards, seq_use.card.subcards[1])
        end
      elseif #cards >= i then
        use = room:askToUseVirtualCard(player,{
          name = card_name,
          skill_name = zhengyun.name,
          prompt = string.format(Fk:translate("#setam__zhengyun-prompt"), i, Fk:translate(card_name)),
          card_filter = {
            n = i,
            cards = cards
          }
        })
        if not use then return end
      end
    end
  end,
  enabled_at_play = Util.TrueFunc,
  enabled_at_response = function(self, player, response)
    return not response
  end
})

zhengyun:addTest(function(room, me)
  local comp2, comp8 = room.players[2], room.players[8]
  local duel = room:printCard("duel")
  local duel2 = room:printCard("duel")
  local snatch = room:printCard("snatch")
  local jink = room:printCard("jink")
  local snatch2 = room:printCard("snatch")
  local slashes = {}
  for _ = 1, 7, 1 do
    table.insert(slashes, room:printCard("slash"))
  end
  FkTest.RunAddSkills(me, zhengyun.name)

  --- test: 发动一次技能，查询每次handler是否满足要求
  --- 期望: 1.只有伤害类牌会满足要求；2.不因此牌获得的伤害类牌不满足要求

  FkTest.setNextReplies(me, {
    FkTest.replyCard(snatch2, { comp8 })
  })

  FkTest.setRoomBreakpoint(me, "PlayCard", FkTest.createClosure(2))

  FkTest.runInRoom(function()
    room:moveCardTo(jink, Card.DrawPile) -- 控顶
    room:moveCardTo(slashes, Card.DrawPile) -- 超级控顶
    comp2:drawCards(3)
    room:obtainCard(me, { duel2, snatch })
    room:obtainCard(comp8, { duel })
    me:gainAnExtraTurn(false, nil, { Player.Play })
  end)

  local handler = ClientInstance.current_request_handler --[[@as ReqPlayCard]]
  lu.assertIsTrue(handler:skillButtonValidity(zhengyun.name))
  handler:selectSkill(zhengyun.name, { selected = true })
  for _, cid in ipairs(me:getCardIds("h")) do
    printf("卡牌%s合法性：%s", Fk:getCardById(cid), handler:cardValidity(cid))
    if cid == snatch.id then
      lu.assertIsFalse(handler:cardValidity(cid))
    else
      lu.assertIsTrue(handler:cardValidity(cid))
    end
  end

  FkTest.setNextReplies(me, {
    FkTest.replyUseSkill(zhengyun.name, { comp2 }, { duel.id })
  })

  FkTest.setRoomBreakpoint(me, "AskForUseActiveSkill", function(data)
    return data[1] == "virtual_viewas"
  end)

  FkTest.resumeRoom()

  local handler2 = ClientInstance.current_request_handler --[[@as ReqActiveSkill]]
  for _, cid in ipairs(me:getCardIds("h")) do
    printf("卡牌%s合法性：%s", Fk:getCardById(cid), handler2:cardValidity(cid))
    if table.contains({ duel2.id, snatch.id }, cid) then
      lu.assertIsFalse(handler2:cardValidity(cid))
    else
      lu.assertIsTrue(handler2:cardValidity(cid))
    end
  end

  FkTest.setNextReplies(me, {
    FkTest.replyUseSkill("virtual_viewas", {}, { me:getCardIds("h")[3] })
  })

  FkTest.setRoomBreakpoint(me, "AskForUseActiveSkill", function(data)
    return data[1] == "virtual_viewas"
  end)

  FkTest.resumeRoom()

  local handle3 = ClientInstance.current_request_handler --[[@as ReqActiveSkill]]
  for _, cid in ipairs(me:getCardIds("h")) do
    printf("卡牌%s合法性：%s", Fk:getCardById(cid), handle3:cardValidity(cid))
    if table.contains({ duel2.id, snatch.id }, cid) then
      lu.assertIsFalse(handle3:cardValidity(cid))
    else
      lu.assertIsTrue(handle3:cardValidity(cid))
    end
  end

  FkTest.setNextReplies(me, {
    FkTest.replyUseSkill("virtual_viewas", { me }, { me:getCardIds("h")[4], me:getCardIds("h")[5] }),
    "reinforcement_nonbasic",
    FkTest.replyDiscard({ duel2.id })
  })

  local used_name = { "snatch", "chasing_near", "ex_nihilo" }

  for i = 1, 2, 1 do
    FkTest.setRoomBreakpoint(me, "AskForUseActiveSkill", function(data)
      return data[1] == "virtual_viewas"
    end)

    FkTest.resumeRoom()

    local handleX = ClientInstance.current_request_handler --[[@as ReqActiveSkill]]
    lu.assertEquals(handleX.extra_data.choices[1], used_name[i])
    local handcards = me:getCardIds("h")
    for _, cid in ipairs(handcards) do
      printf("卡牌%s合法性：%s", Fk:getCardById(cid), handleX:cardValidity(cid))
      -- if table.contains({ duel2.id, snatch.id }, cid) then
      --   lu.assertIsFalse(handle3:cardValidity(cid))
      -- else
      --   lu.assertIsTrue(handle3:cardValidity(cid))
      -- end
    end

    FkTest.setNextReplies(me, {
      FkTest.replyUseSkill("virtual_viewas", {}, { handcards[#handcards - i] })
    })
  end

  FkTest.setRoomBreakpoint(me, "AskForUseActiveSkill", function(data)
    -- p(data)
    return data[1] == "virtual_viewas"
  end)

  FkTest.resumeRoom()

  local handleX = ClientInstance.current_request_handler --[[@as ReqActiveSkill]]
  lu.assertEquals(handleX.extra_data.choices[1], used_name[3])
  local handcards = me:getCardIds("h")
  for _, cid in ipairs(handcards) do
    printf("卡牌%s合法性：%s", Fk:getCardById(cid), handleX:cardValidity(cid))
    lu.assertIsFalse(handle3:cardValidity(cid))
  end
end)

Fk:loadTranslationTable{
  ["setam__zhengyun"] = "征云",
  [":setam__zhengyun"] = "你可以将一张伤害类牌当【逐近弃远】使用；"
  .."然后可以将以上牌获得的一张伤害类牌当【无中生有】使用；"
  .."然后可以将以上牌获得的两张伤害类牌当【增兵减灶】使用；"
  .."然后可以将以上牌获得的三张伤害类牌依次当你本回合使用的前三张普通锦囊牌使用。",

  ["#setam__zhengyun-prompt"] = "征云：可以将%s张伤害类牌当【%s】使用",
}

return zhengyun
